﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Watson.Core.Tools;
using ITOMMS.Entity;
using ITOMMS.Business;

namespace ITOMMS.Web.Contract
{
    public partial class ContractAdd : BasePage
    {
        public string sAttachment = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtJudgementDate.Text = DateTime.Now.ToShortDateString();
                DropDownListHelper.InitDropList<tbUserEntity>(new tbUserEntity().GetAllByQuery("RoleGroup=" + (int)PubEnums.RoleGroup.客户), ddlCustomer, "ID", "CompanyName");//绑定公司
                DropDownListHelper.InitDropList<tbContractServiceClassEntity>(new tbContractServiceClassEntity().GetAll(), ddlServiceType, "ID", "ClassName");//绑定服务类别
                if (Request.Params["id"] != null)
                {
                    string sid = Request.Params["id"].ToString();
                    int pid = 0;
                    if (int.TryParse(sid, out pid))
                    {
                        titleInfo.Text = "合同编辑";
                        DataShow(pid);
                    }
                }
                if (Request.Params["vid"] != null)
                {
                    string sid = Request.Params["vid"].ToString();
                    int pid = 0;
                    if (int.TryParse(sid, out pid))
                    {
                        titleInfo.Text = "合同查看";
                        DataShow(pid);

                        txtContractNo.ReadOnly = true;
                        txtEffectiveTime.ReadOnly = true;
                        txtExpirationTime.ReadOnly = true;
                        txtJudgementDate.ReadOnly = true;
                        txtRemarks.ReadOnly = true;
                        ddlCustomer.Enabled = false;
                        ddlServiceType.Enabled = false;
                    }
                }
            }
        }
        private void DataShow(int troubleId)
        {
            tbContractEntity entity = new tbContractEntity();
            entity = entity.GetById(troubleId);

            if (entity != null)
            {
                ViewState["Entity"] = entity;
                //txtContractNo.Enabled = false;
                txtContractNo.Text = entity.ContractNo;
                try
                {
                    txtEffectiveTime.Text = entity.EffectiveTime.Value.ToShortDateString();
                    txtExpirationTime.Text = entity.ExpirationTime.Value.ToShortDateString();
                    txtJudgementDate.Text = entity.JudgementDate.Value.ToShortDateString();
                }
                catch
                {
                }
              
                txtRemarks.Text = entity.Remarks;
                ddlCustomer.SelectedValue = entity.FrstParty.ToString();
                ddlServiceType.SelectedValue = entity.ServiceID.ToString();
                this.ddl提醒类别.SelectedValue = entity.提醒类别.ToString();
                this.txt提醒日期.Text = entity.提醒日期.ToString();

                DataSet dsTemp = new tbAppendixEntity().GetDataSet(" select NewFileName from tb_Appendix where  BusinessID=" + entity.ID.ToString() + "");
                if (dsTemp.Tables[0].Rows.Count > 0)
                {
                    sAttachment = dsTemp.Tables[0].Rows[0]["NewFileName"].ToString();
                    Label1.Visible = true;
                }     

            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Entity.tbContractEntity entity = new ITOMMS.Entity.tbContractEntity();
                if (ViewState["Entity"] != null)
                {
                    entity = ViewState["Entity"] as tbContractEntity;
                }
                else
                {
                    entity.CreateBy = SessionHelper.UserID;
                    entity.CreateDate = DateTime.Now;
                }

                entity.ContractNo = txtContractNo.Text;
                entity.Remarks = txtRemarks.Text;

                try
                {
                    entity.FrstParty = int.Parse(ddlCustomer.SelectedValue);
                    entity.ServiceID = int.Parse(ddlServiceType.SelectedValue);
                }
                catch 
                {
                    
                    throw;
                }
            
                entity.提醒类别 = this.ddl提醒类别.SelectedValue;
                //entity.提醒日期 = this.txt提醒日期.Text.Trim();

                DateTime tx = new DateTime();
                if (DateTime.TryParse(txt提醒日期.Text.Trim(), out  tx))
                {
                    entity.提醒日期 = tx;
                }

                DateTime et = new DateTime();
                if (DateTime.TryParse(txtEffectiveTime.Text.Trim(), out  et))
                {
                    entity.EffectiveTime = et;
                }

                DateTime ext = new DateTime();
                if (DateTime.TryParse(txtExpirationTime.Text.Trim(), out  ext))
                {
                    entity.ExpirationTime = ext;
                }
                DateTime jd = new DateTime();
                if (DateTime.TryParse(txtJudgementDate.Text.Trim(), out  jd))
                {
                    entity.JudgementDate = jd;
                }

                entity.ModifyBy = SessionHelper.UserID;
                entity.ModifyDate = DateTime.Now;
                entity.SaveOrUpdate(entity);
                SaveImgToServer(FileUpload1, entity.ID);
                ViewState["Entity"] = entity;
                MessageboxInfo.MessageBox(this, MessageType.SUCCEEDED, "保存成功");
            }
            catch (Exception ex)
            {
                log4netHelper.error(ex);
                MessageboxInfo.MessageBox(this, MessageType.ERROR, "保存失败");
            }
        }



        private void SaveImgToServer(FileUpload imageUpload, int ID)
        {
            string rs = string.Empty;
            try
            {
                if (!imageUpload.HasFile)
                {
                    return;
                }
                //更新处理
                tbAppendixEntity entity = new tbAppendixEntity();
                entity = entity.FindEntity("BusinessID=" + ID);

                string year = System.DateTime.Now.Year.ToString();
                string month = System.DateTime.Now.Month.ToString();
                string day = System.DateTime.Now.Day.ToString();
                string hour = System.DateTime.Now.Hour.ToString();
                string minute = System.DateTime.Now.Minute.ToString();
                string second = System.DateTime.Now.Second.ToString();
                string fileName = year + month + day + hour + minute + second;

                string filePath = imageUpload.PostedFile.FileName;
                string fileEx = filePath.Substring(filePath.LastIndexOf(".") + 1).ToLower();
                if (!System.IO.Directory.Exists(Server.MapPath(@"~/UpLoadFiles\")))
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath(@"~/UpLoadFiles\"));
                }


                //大小图
                string serverpath = Server.MapPath(@"~/UpLoadFiles\") + fileName + "." + fileEx;
                imageUpload.PostedFile.SaveAs(serverpath);

                rs = fileName + "." + fileEx;
                if (entity == null)
                {
                    entity = new tbAppendixEntity();
                }
                else
                {
                    //rs = entity.NewFileName;
                }

                entity.NewFileName = rs;
                entity.FileType = fileEx;
                entity.OriginalFileName = imageUpload.FileName;
                entity.Remark = "合同附件";
                entity.BusinessType = "合同";
                entity.BusinessID = ID;
                entity.SaveOrUpdate(entity);

            }
            catch
            {
                MessageBoxHelper.PopUpMessage("附件上传失败");
            }

        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("ContractList.aspx", true);
        }
    }
}
